package cn.ibizlab.odoo.core.odoo_base.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Ires_country;
import cn.ibizlab.odoo.core.odoo_base.domain.Res_country;
import cn.ibizlab.odoo.core.odoo_base.filter.Res_countrySearchContext;
import cn.ibizlab.odoo.core.odoo_base.service.IRes_countryService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[国家/地区] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Res_countryServiceImpl implements IRes_countryService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Ires_countryClientService res_countryClientService;

    @Override
    public boolean update(Res_country res_country) {
        boolean bOk = false ;
        Ires_country clientModel = this.convert2Model(res_country,null);
        res_countryClientService.update(clientModel) ;
        this.convert2Domain(clientModel,res_country) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public Res_country get(Integer id) {
        Ires_country clientModel = res_countryClientService.createModel();
        clientModel.setId(id);
        res_countryClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Res_country get(Res_country res_country) {
        Res_country retres_country = this.get(res_country.getId()) ;
    	BeanCopier copier=BeanCopier.create(retres_country.getClass(), res_country.getClass(), false);
        copier.copy(retres_country, res_country, null);
        return res_country ;
    }

    @Override
    public boolean create(Res_country res_country) {
        boolean bOk = false ;
        Ires_country clientModel = this.convert2Model(res_country,null);
        res_countryClientService.create(clientModel) ;
        this.convert2Domain(clientModel,res_country);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void updateBatch(List<Res_country> res_countrys, int batchSize) {

        //res_countryClientService.updateBatch(res_countrys,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Ires_country clientModel = res_countryClientService.createModel();
        clientModel.setId(id);
        res_countryClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void createBatch(List<Res_country> res_countrys, int batchSize) {

                        //res_countryClientService.createBatch(res_countrys,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //res_countryClientService.removeBatch(res_countrys) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Res_country> searchDefault(Res_countrySearchContext context) {
        List<Res_country> list = new ArrayList<Res_country>();
        Page<cn.ibizlab.odoo.core.client.model.Ires_country> clientModelList = res_countryClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ires_country clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Res_country>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据币种查询
     */
    @Override
    public Page<Res_country> selectByOdoo_currency(Integer id){
        Res_countrySearchContext context = new Res_countrySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Res_country> list = new ArrayList<Res_country>();
        Page<cn.ibizlab.odoo.core.client.model.Ires_country> clientModelList = res_countryClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ires_country clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Res_country>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Res_country> selectByOdoo_create(Integer id){
        Res_countrySearchContext context = new Res_countrySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Res_country> list = new ArrayList<Res_country>();
        Page<cn.ibizlab.odoo.core.client.model.Ires_country> clientModelList = res_countryClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ires_country clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Res_country>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Res_country> selectByOdoo_write(Integer id){
        Res_countrySearchContext context = new Res_countrySearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Res_country> list = new ArrayList<Res_country>();
        Page<cn.ibizlab.odoo.core.client.model.Ires_country> clientModelList = res_countryClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ires_country clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Res_country>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Ires_country convert2Model(Res_country domain , Ires_country model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = res_countryClientService.createModel() ;
        }

         if(domain.getAddress_view_idDirtyFlag())
            model.setAddress_view_id(domain.getAddress_view_id());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getImageDirtyFlag())
            model.setImage(domain.getImage());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getCountry_group_idsDirtyFlag())
            model.setCountry_group_ids(domain.getCountry_group_ids());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getState_idsDirtyFlag())
            model.setState_ids(domain.getState_ids());
         if(domain.getName_positionDirtyFlag())
            model.setName_position(domain.getName_position());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getPhone_codeDirtyFlag())
            model.setPhone_code(domain.getPhone_code());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getCodeDirtyFlag())
            model.setCode(domain.getCode());
         if(domain.getVat_labelDirtyFlag())
            model.setVat_label(domain.getVat_label());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getAddress_formatDirtyFlag())
            model.setAddress_format(domain.getAddress_format());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getCurrency_id_textDirtyFlag())
            model.setCurrency_id_text(domain.getCurrency_id_text());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getCurrency_idDirtyFlag())
            model.setCurrency_id(domain.getCurrency_id());
        return model ;
    }

    public Res_country convert2Domain( Ires_country model ,Res_country domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Res_country();
        }

        if(model.getAddress_view_idDirtyFlag())
            domain.setAddress_view_id(model.getAddress_view_id());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getImageDirtyFlag())
            domain.setImage(model.getImage());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getCountry_group_idsDirtyFlag())
            domain.setCountry_group_ids(model.getCountry_group_ids());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getState_idsDirtyFlag())
            domain.setState_ids(model.getState_ids());
        if(model.getName_positionDirtyFlag())
            domain.setName_position(model.getName_position());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getPhone_codeDirtyFlag())
            domain.setPhone_code(model.getPhone_code());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getCodeDirtyFlag())
            domain.setCode(model.getCode());
        if(model.getVat_labelDirtyFlag())
            domain.setVat_label(model.getVat_label());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getAddress_formatDirtyFlag())
            domain.setAddress_format(model.getAddress_format());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getCurrency_id_textDirtyFlag())
            domain.setCurrency_id_text(model.getCurrency_id_text());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getCurrency_idDirtyFlag())
            domain.setCurrency_id(model.getCurrency_id());
        return domain ;
    }


}
